#!/usr/bin/env node
var util  = require('util'),
    // shelljs = require('shelljs/global'),
    spawn = require('child_process').spawn,
    exec = require('child_process').exec,
    isWin = /^win/.test(process.platform);
    // p = require('procstreams'),
    // argv = require('optimist').argv;

    var argv = process.argv.splice(2)
    // var cmd =  argv.$0;
    var cmd =  argv[0];
    // var args = argv.argv
    var args = argv[1];
    // var option = argv.option
    var option = argv[2];


    switch (cmd) {
        case 'dev':
            dev(cmd, args, option)
            break;
        case 'pro':
            pro(cmd, args, option)
            break;
        case 'install':
            install(cmd, args, option)
            break;
        default:

    }

    function dev(cmd, args, option){
        console.log('========== dev');
        console.log('========== dev');
        console.log('========== dev');
        var port = args && typeof args==='string' || false;
        var rundev = './ly dev' + (port = port ? ' '+port:'')

        var _dev = spawn('./ly'/*command*/, ['dev', port]/*args*/, {}/*options, [optional]*/);
        _dev.stdout.on('data', function (data) {
            console.log('stdout: ' + data);
        });

        _dev.stderr.on('data', function (data) {
            console.log('stderr: ' + data);
        });

        _dev.on('exit', function (code) {
            console.log('child process exited with code ' + code);
        });

        // isWin
        // ? exec(rundev)
        // : exec('sudo '+rundev)
    }

    function pro(cmd, args, option){
        console.log('========== pro');
        console.log('========== pro');
        console.log('========== pro');
        var port = args && typeof args==='string' || false;
        var runpro = './ly pro' + (port = port ? ' '+port:'')

        var _pro = spawn('./ly'/*command*/, ['pro', port]/*args*/, {}/*options, [optional]*/);
        _pro.stdout.on('data', function (data) {
            console.log('stdout: ' + data);
        });

        _pro.stderr.on('data', function (data) {
            console.log('stderr: ' + data);
        });

        _pro.on('exit', function (code) {
            console.log('child process exited with code ' + code);
        });

        // isWin
        // ? exec(runpro)
        // : exec('sudo '+runpro)
    }

    function install(cmd, args, option){
        console.log('========== install');
        console.log('========== install');
        console.log('========== install');

        var _ins

        if (isWin){
            _ins = spawn('./ly'/*command*/, ['install3']/*args*/, {}/*options, [optional]*/);
        }
        else {
            _ins = spawn('./ly'/*command*/, ['install']/*args*/, {}/*options, [optional]*/);
        }

        _ins.stdout.on('data', function (data) {
            console.log('stdout: ' + data);
        });

        _ins.stderr.on('data', function (data) {
            console.log('stderr: ' + data);
        });

        _ins.on('exit', function (code) {
            console.log('child process exited with code ' + code);
        });

        // isWin
        // ? exec('./ly install3')
        // : exec('./ly install')
    }


// spawn('cd', ['./public'])
// var ls = spawn('pwd')

// p('cat app.log').pipe('wc -l')
//   .data(function(stdout, stderr) {
//       console.log(stdout);
//   });

// p('mkdir foo')
//        .and('rm file.txt')
//           .on('exit', function() {
//                 console.log('done');
//            });



// var ls    = spawn(cmd , [args ,option]);
// ls    = spawn('du', ['-h','/home/'])

// var start = +new Date();
// ls.stdout.on('data', function (data) {
//     console.log('stdout: ' + data);
// });
//
// ls.stderr.on('data', function (data) {
//     console.log('stderr: ' + data);
// });
//
// ls.on('exit', function (code) {
//     // var end = + new Date();
//     // console.log(end-start);
// });
